package com.leenmvc.core.dao.iotdb.util;

import com.leenmvc.core.exception.BusinessException;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.read.common.Field;

public class IotDbUtil {

    /**
     * 读取iotdb返回Field类型的参数值
     * @param field
     * @return
     */
    public static Object getValue(Field field){
        TSDataType dataType = field.getDataType();
        Object o = null;
        switch (dataType){
            case TEXT:
                o = field.getStringValue();
                break;
            case FLOAT:
                o = field.getFloatV();
                break;
            case INT32:
                o = field.getIntV();
                break;
            case INT64:
                o = field.getLongV();
                break;
            case DOUBLE:
                o = field.getDoubleV();
                break;
            case BOOLEAN:
                o = field.getBoolV();
                break;
            default:
                o = field.getStringValue();
        }
        return o;
    }

    /**
     * 通过前端传过来的dataType判断iotdb的dataType
     */
    public static TSDataType dataTypeChange(String dataType){
        TSDataType tsDataType = null;
        switch (dataType){
            case "1":
                tsDataType = TSDataType.BOOLEAN;
                break;
            case "2":
                tsDataType = TSDataType.INT32;
                break;
            case "3":
                tsDataType = TSDataType.INT64;
                break;
            case "4":
                tsDataType = TSDataType.FLOAT;
                break;
            case "5":
                tsDataType = TSDataType.DOUBLE;
                break;
            case "6":
                tsDataType = TSDataType.TEXT;
                break;
            default:
                throw new BusinessException("iotdb数据转化异常");
        }
        return tsDataType;
    }
    /**
     * 通过类型，判断该选择哪种编码方式
     */
    public static TSEncoding optionTSEncoding(TSDataType dataType ){
        TSEncoding tsEncoding = null;
        if (dataType == TSDataType.BOOLEAN || dataType == TSDataType.TEXT || dataType == TSDataType.INT32 || dataType == TSDataType.INT64){
            tsEncoding = TSEncoding.PLAIN;
        }else {
            tsEncoding = TSEncoding.GORILLA;
        }
        return tsEncoding;
    }
}
